/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.dialog;

import cn.ac.nercis.pes.community.support.controls.JFXDialog;
import cn.ac.nercis.pes.community.utils.GuiStore;
import cn.ac.nercis.pes.community.utils.IconUtils;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import lombok.Getter;
import lombok.Setter;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 提示信息框组件
 * @author zpy
 * @version v1.0.0
 */
public class MessageDialog {
    private final JFXDialog dialog;

    @Getter
    private MessageType type = MessageType.SUCCESS;
    @Getter
    @Setter
    private String message;
    @Getter
    @Setter
    private StackPane dialogContainer;

    public MessageDialog(){
        dialog = new JFXDialog();
        dialogContainer = GuiStore.getRoot();
    }

    private MessageDialog(Builder builder){
        this();
        type = builder.type;
        message = builder.message;
        dialogContainer = builder.dialogContainer;
    }

    public void show(){
        this.render();
        this.dialog.show();
        autoClose();
    }

    private void render(){
        this.dialog.setDialogContainer(dialogContainer);
        HBox container = new HBox();
        container.setAlignment(Pos.CENTER_LEFT);
        container.setSpacing(10);
        Label iconLabel = new Label();
        iconLabel.setContentDisplay(ContentDisplay.CENTER);
        if(type == MessageType.WARN){
            container.getStyleClass().add("pes-message-warn");
            iconLabel.getStyleClass().addAll("message-icon");
            iconLabel.setGraphic(IconUtils.getGlyph("message-warn-icon","message-dialog-icon"));
        }else if(type == MessageType.FAIL){
            container.getStyleClass().add("pes-message-error");
            iconLabel.getStyleClass().addAll("message-icon");
            iconLabel.setGraphic(IconUtils.getGlyph("message-error-icon","message-dialog-icon"));
        }else {
            container.getStyleClass().add("pes-message-success");
            iconLabel.getStyleClass().addAll("message-icon");
            iconLabel.setGraphic(IconUtils.getGlyph("message-success-icon","message-dialog-icon"));
        }
        container.getChildren().add(iconLabel);
        Label contentLabel = new Label(message);
        contentLabel.getStyleClass().add("message-content");
        container.getChildren().add(contentLabel);

        HBox closeContainer = new HBox();
        HBox.setHgrow(closeContainer, Priority.SOMETIMES);
        closeContainer.setAlignment(Pos.CENTER_RIGHT);
        closeContainer.setPadding(new Insets(0,0,0,20));
        Button closeButton = new Button();
        closeButton.setMnemonicParsing(false);
        closeButton.setOnAction(event -> this.dialog.close());
        closeButton.getStyleClass().add("message-close-btn");
        closeButton.setGraphic(IconUtils.getGlyph("message-close-icon","message-close-btn-icon"));
        closeContainer.getChildren().add(closeButton);
        container.getChildren().add(closeContainer);
        this.dialog.setContent(container);
    }

    private void autoClose(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(dialog.isVisible()){
                    Platform.runLater(dialog::close);
                }
            }
        }, 5000);
    }

    public static Builder builder(){
        return new Builder();
    }

    public static final class Builder {
        private MessageType type;
        private String message;
        private StackPane dialogContainer;

        private Builder(){
            type = MessageType.SUCCESS;
            dialogContainer = GuiStore.getRoot();
        }

        public Builder withType(MessageType type){
            this.type = type;
            return this;
        }

        public Builder withMessage(String message){
            this.message = message;
            return this;
        }

        public Builder withContainer(StackPane dialogContainer){
            this.dialogContainer = dialogContainer;
            return this;
        }

        public MessageDialog build(){
            return new MessageDialog(this);
        }
    }
}